<template>
    <div id="login" :style="style">
        <el-card class="my-card">
            <div slot="header" class="clearfix" shadow="always">
                <span>登录</span>
            </div>
            <el-form ref="login_form" class="edit-el-form" :model="login_form" :rules="rules" label-width="80px">
                <el-form-item label="账号" prop="username">
                    <label slot="label">帐&nbsp;&nbsp;&nbsp;&nbsp;号</label>
                    <el-input v-model="login_form.username" placeholder="请输入账号" clearable required></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <label slot="label">密&nbsp;&nbsp;&nbsp;&nbsp;码</label>
                    <el-input v-model="login_form.password" placeholder="请输入密码" show-password clearable required></el-input>
                </el-form-item>
                <el-form-item label="验证码" id="verify-div" prop="verifyCode">
                    <el-input class="verify_css" id="verify-input-div" placeholder="请输入验证码" v-model="login_form.verifyCode"></el-input>
                    <!--关键 ↓-->
                    <div id="v_container"></div>
                </el-form-item>
            </el-form>
            <div style="margin-left: 80px;">
                <el-button style="width:280px" type="primary" @click="submitForm('login_form')" :loading="loading">{{button_name}}</el-button>
            </div>
        </el-card>

    </div>
</template>

<script>
    import { GVerify } from '../../assets/js/verifyCode.js';
    import { login } from '@/api/login.js';
    export default {
        name: 'Login',
        data() {
            let validateRequire=(rule,value,callback)=>{
                let require_arr={
                   username:'账号',
                   password:'密码',
                   verifyCode:'验证码'
                };
                let rule_name=require_arr[rule.field];
                if (value === '') {
                    callback(new Error('请输入'+rule_name));
                 }else{
                    callback();
                 }
            }
            return {
                login_form: {
                    username: '',
                    password: '',
                    verifyCode: '',
                },
                //校验规则
                rules:{
                    username:[
                       { validator:validateRequire,trigger:'blur' }
                    ],
                    password:[
                       { validator:validateRequire,trigger:'blur' }
                    ],
                    verifyCode:[
                       { validator:validateRequire,trigger:'blur' }
                    ]
                },
                loading:false,
                button_name:'登录',
                style:'',
                verifyCode:null
            }
        },
        created(){

        },
        mounted(){
            this.verifyCode = new GVerify('v_container');
        },
        methods: {
            submitForm(formName) {
                let that = this;
                this.$refs[formName].validate((valid)=>{
                    if (valid) {
                        let verifyFlag=that.verifyCode.validate(that.login_form.verifyCode);
                        if(!verifyFlag){
                            that.$message({
                                message:'验证码错误',
                                type:'error',
                                center: true,
                                duration:1000,
                            });
                            return false;
                        }
                        that.loading=true;
                        that.button_name='正在登录';
                        let send_data={
                            username: this.login_form.username,
                            password: this.login_form.password
                        };
                        login(send_data).then(function(response){
                            let type='error'
                            let code=response.data.code;
                            if(code==1){
                               type='success';
                               that.$store.dispatch('loadLogin',response.data.info);
                            }
                            that.$message({
                                message:response.data.msg,
                                type:type,
                                duration:1200,
                                onClose:function(e){
                                   that.loading=false;
                                   that.button_name='登录';
                                   if(code==1){
                                      that.$router.push('/main');
                                   }else{
                                      that.login_form.verifyCode='';
                                      that.verifyCode.refresh();
                                   }
                                }
                            });
                        })
                    } else {
                        return false;
                     }
                })
            }
        }
    }
</script>

<style>
@import url('../../assets/css/login.css');

</style>
